package org.wesc.boot.webapi.api.system;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.wesc.boot.common.api.LoginResultCode;
import org.wesc.boot.common.api.R;
import org.wesc.boot.common.utils.RandomUtil;
import org.wesc.boot.dao.model.UserResultModel;
import org.wesc.boot.dao.redis.RedisTemplateService;
import org.wesc.boot.service.sms.AliSmsEnum;
import org.wesc.boot.service.sms.AliSmsService;
import org.wesc.boot.service.system.user.UserAdvanceService;

/**
 * @author Wesley
 */
@Api(tags = {"短信接口"})
@Slf4j
@RestController
@RequestMapping("/api")
public class SmsApi {

    private final UserAdvanceService userAdvanceService;

    private final RedisTemplateService redisTemplateService;

    private final AliSmsService aliSmsService;

    @Autowired
    public SmsApi(UserAdvanceService userAdvanceService, RedisTemplateService redisTemplateService, AliSmsService aliSmsService) {
        this.userAdvanceService = userAdvanceService;
        this.redisTemplateService = redisTemplateService;
        this.aliSmsService = aliSmsService;
    }

    /**
     * 短信验证码发送接口
     * @return 短信发送结果
     */
    @PostMapping(value = "/sms")
    public R<String> sms(
            @RequestParam("mobile") String mobile,
            @RequestParam("smsMode") String smsMode) throws Exception {
        // 登陆时需要先校验手机号有效性，不存在则不发送验证码
        UserResultModel userResultModel = userAdvanceService.findDetailByMobile(mobile);
        if (null == userResultModel) {
            return R.fail(LoginResultCode.INVALID_MOBILE);
        }

        //随机数
        String captcha = RandomUtil.randomNumbers(6);
        JSONObject message = new JSONObject();
        message.put("code", captcha);

        try {
            boolean result;
            // smsMode 短信模板方式  0 .登录模板、1.注册模板、2.忘记密码模板
            if (smsMode.equals(AliSmsEnum.LOGIN_TEMPLATE_CODE.getMode())) {
                // 登录模板
                result = aliSmsService.sendSms(mobile, message, AliSmsEnum.LOGIN_TEMPLATE_CODE);
            } else if(smsMode.equals(AliSmsEnum.REGISTER_TEMPLATE_CODE.getMode())) {
                // 注册模板
                result = aliSmsService.sendSms(mobile, message, AliSmsEnum.REGISTER_TEMPLATE_CODE);
            } else if(smsMode.equals(AliSmsEnum.FORGET_TEMPLATE_CODE.getMode())) {
                // 忘记密码模板
                result = aliSmsService.sendSms(mobile, message, AliSmsEnum.FORGET_TEMPLATE_CODE);
            } else {
                result = false;
            }

            // 短信异常
            if (!result) {
                return R.fail(LoginResultCode.SMS_ERROR);
            }

            // 验证码10分钟内有效，保存于redis中
            redisTemplateService.set(mobile, captcha, 600);
            return R.success("短信验证码发送成功");
        } catch (ClientException e) {
            log.error(e.getErrMsg());
            return R.fail("短信接口开通失败，请联系管理员");
        }
    }

}
